package webServlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.user.domain.Detialed;
import cn.user.domain.Food;
import cn.user.domain.dao.DetialDao;
import cn.user.domain.dao.FoodDao;

/**
 * Servlet implementation class addDataServlet
 */
@WebServlet("/addDataServlet")
public class addDataServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(request.getParameter("cityid")==null||request.getParameter("cityid")=="")
		{
			int foodid = Integer.parseInt(request.getParameter("foodid"));
			String dname = request.getParameter("foodname");
			String detial = request.getParameter("detial");
			String buyurl = request.getParameter("buyurl");
			String history = request.getParameter("history");
			String charactor = request.getParameter("charactor");
			String domake = request.getParameter("domake");
			String img1 = request.getParameter("img1");
			String img2 = request.getParameter("img2");
			String img3 = request.getParameter("img3");
			Detialed de = new Detialed(foodid, dname, detial, buyurl, history, charactor, domake, img1, img2, img3);
			new DetialDao().add(de);
			response.sendRedirect(request.getContextPath()+"/addSuccess.html");
			return;
		}
		System.out.println("222");
		int cityid = Integer.parseInt(request.getParameter("cityid"));
		int foodid = Integer.parseInt(request.getParameter("foodid"));
		String dname = request.getParameter("foodname");
		String foodinf = request.getParameter("foodinf");
		String fimg1 = request.getParameter("fimg1");
		String fimg2 = request.getParameter("fimg2");
		String fimg3 = request.getParameter("fimg3");
		String fimg4 = request.getParameter("fimg4");
		String detial = request.getParameter("detial");
		String buyurl = request.getParameter("buyurl");
		String history = request.getParameter("history");
		String charactor = request.getParameter("charactor");
		String domake = request.getParameter("domake");
		String img1 = request.getParameter("img1");
		String img2 = request.getParameter("img2");
		String img3 = request.getParameter("img3");
		Detialed de = new Detialed(foodid, dname, detial, buyurl, history, charactor, domake, img1, img2, img3);
		Food food = new Food(foodid, dname, foodinf, fimg1, fimg2, fimg3, fimg4, cityid);
		new FoodDao().add(food);
		new DetialDao().add(de);
		response.sendRedirect(request.getContextPath()+"/addSuccess.html");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
